package com.yiqi.news.mvp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import butterknife.OnClick

import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.jess.arms.widget.DividerItemDecoration
import com.novel.cn.ext.dp2px

import com.yiqi.news.di.component.DaggerBankcardComponent
import com.yiqi.news.di.module.BankcardModule
import com.yiqi.news.mvp.contract.BankcardContract
import com.yiqi.news.mvp.presenter.BankcardPresenter

import com.yiqi.news.R
import com.yiqi.news.entity.BankTypeVO
import com.yiqi.news.mvp.ui.adapter.LabelAdapter
import com.yiqi.news.mvp.ui.adapter.TypeAdapter
import kotlinx.android.synthetic.main.activity_bankcard.*
import kotlinx.android.synthetic.main.dialog_bottom_sheet_label.view.*

class BankcardActivity : BaseActivity<BankcardPresenter>(), BankcardContract.View, TextWatcher {
    private lateinit var mBankData: BankTypeVO

    private val mBottomSheetDialog: BottomSheetDialog by lazy {
        return@lazy BottomSheetDialog(this)
    }

    private val mBatchAdapter: TypeAdapter by lazy {
        return@lazy TypeAdapter()
    }

    override fun showCardTypeSuccess(bank: List<BankTypeVO>) {
        mBatchAdapter.setNewData(bank)
        mBottomSheetDialog.show()
    }

    override fun afterTextChanged(s: Editable?) {
        btn_backcard_add.isEnabled = (edt_backcard_name.text.isNotEmpty() && edt_backcard_no.text.isNotEmpty()
                && edt_backcard_username.text.isNotEmpty() && edt_backcard_id_number.text.isNotEmpty())

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun showAddCardError(errMsg: String) {
        showMessage(errMsg)
    }

    override fun showAddCardSuccess() {
        showMessage("添加成功")
        finish()
    }

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerBankcardComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .bankcardModule(BankcardModule(this))
                .build()
                .inject(this)
    }


    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_bankcard //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    override fun initData(savedInstanceState: Bundle?) {
        initBottomSheetDialog()
    }

    override fun initEvent() {
        edt_backcard_name.addTextChangedListener(this)
        edt_backcard_no.addTextChangedListener(this)
        edt_backcard_username.addTextChangedListener(this)
        edt_backcard_id_number.addTextChangedListener(this)
    }


    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {
        finish()
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(R.id.toolbar).init()
    }

    @OnClick(R.id.btn_backcard_add, R.id.edt_backcard_name)
    fun onClick(view: View) {
        when (view.id) {
            R.id.btn_backcard_add -> mPresenter?.doAddCard(
                    mBankData,
                    edt_backcard_no.text.toString(),
                    1,
                    edt_backcard_username.text.toString(),
                    edt_backcard_id_number.text.toString())
            R.id.edt_backcard_name -> mPresenter?.doGetBanklist()
        }
    }

    private fun initBottomSheetDialog() {
        var view = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_sheet_label, null, false)
        view.recycler_view.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(dp2px(4)))
        mBatchAdapter.bindToRecyclerView(view.recycler_view)
        mBottomSheetDialog.setContentView(view)
        (view.parent as View).setBackgroundColor(resources.getColor(android.R.color.transparent))
        view.tv_dialog_close.setOnClickListener { mBottomSheetDialog.dismiss() }
        mBatchAdapter.setOnItemClickListener { adapter, view, position ->
            mBankData = mBatchAdapter.getItem(position)!!
            edt_backcard_name.text = mBankData.bankName
            mBottomSheetDialog.dismiss()
        }
    }

}
